#include "configator.h"
#include "config.h"

#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <string.h>
#include <cstdio>
#include <unistd.h>

#ifndef DEBUG
#define INT_LANG DATADIR "/lang/%s.ini"
#define INT_CONFIG "/etc/tlg/config.ini"
#else 
#define INI_LANG "../res/lang/%s.ini"
#define INI_CONFIG "../res/config.ini"
#endif // DEBUG

#define IF_COND_IS_NULL(X) if ((X) == NULL)  
#define IF_COND_NOT_NULL(X) if ((X) != NULL)
static void lang_handle(void* data, char** pars, const int pars_count)
{
  IF_COND_NOT_NULL(*((char**)data)) {
    free(*((char**)data));
  }

  // else
  *((char**)data) = strdup(*pars);
}

static void config_handle_u8(void* data, char** pars, const int pars_count)
{
  if (strcmp(*pars, "") == 0) {
    *((uint8_t*)data) = 0;
  }
  else
  {
    *((uint8_t*)data) = atoi(*pars);
  }
}

static void config_handle_u16(void* data, char** pars, const int pars_count)
{
  if (strcmp(*pars, "") == 0)
  {
    *((uint16_t*)data) = 0;
  }
  else
  {
    *((uint16_t*)data) = atoi(*pars);
  }
}

void config_handle_str(void* data, char** pars, const int pars_count)
{
  IF_COND_NOT_NULL(*((char**)data)) {
    free(*((char**)data));
  }

  *((char**)data) = strdup(*pars);
}

static void config_handle_bool(void* data, char** pars, const int pars_count)
{
  *((bool*)data) = (strcmp("true", *pars) == 0);
}

void lang_load()
{
  //TODO: big handle load method

// ------------ define datas
uint16_t map_len[] = {45};
struct configator_param* map[] = {
  map_no_section,
};
// ------------ end define datas

}

void config_load(const char* config_path)
{
  IF_COND_IS_NULL(cfg_path) {
    config_path = INI_CONFIG;
  }
  // TODO:
}

